<?php
namespace Qy {
    /**
    * 企业微信卡券服务类
    * @author Jamers
    * @since 2017.1.1
    * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E5%88%9B%E5%BB%BA%E5%8D%A1%E5%88%B8
    */
    class Card {
        private $common;
        
        public function __construct($ary = array()) {
            if (isset($ary['common'])) {
                $this->common = &$ary['common'];
            }else{
                $this->common = new \Qy\Common();
            }
        }
        
        /**
        * 上传卡券LOGO
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E5%88%9B%E5%BB%BA%E5%8D%A1%E5%88%B8
        * 
        * @param mixed $path
        * @param mixed $a_token
        */
        public function uploadCardLogo($path,$a_token = '') {
            if (empty($path)) return false;
            if (!file_exists($path)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $data = array('media' => '@' . ZW_ROOT . '/' . ltrim($path, '/'));
            $url = "https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg?access_token={$a_token}&type=card_logo";
            return $this->common->uploadFileByPost($url, $data);
        }
        
        /**
        * 创建卡券
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E5%88%9B%E5%BB%BA%E5%8D%A1%E5%88%B8
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function createCard($ary = array(), $a_token = '') {
            if (empty($ary)) return false;
            if (!is_array($ary) || !isset($ary['card']) || !is_arry($ary['card'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/card/create?access_token={$a_token}";
            return $this->common->httpRequest($url,$ary,'post');
        }
        
        /**
        * 获取卡券详情
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E5%8D%A1%E5%88%B8
        * 
        * @param mixed $card_id
        * @param mixed $a_token
        */
        public function getCard($card_id, $a_token = '') {
            if (empty($card_id)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $data = array('card_id' => $card_id);
            $url = "https://qyapi.weixin.qq.com/cgi-bin/card/get?access_token={$a_token}";
            return $this->common->httpRequest($url,$data,'post');
        }
        
        /**
        * 获取卡券摘要列表
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E5%8D%A1%E5%88%B8
        * 
        * @param mixed $offset
        * @param mixed $count
        * @param mixed $status
        * @param mixed $a_token
        */
        public function batchGetCard($offset, $count, $status = '', $a_token = '') {
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $data = array('offset' => $offset, 'count' => $count);
            if ($status) $data['status'] = $status;
            $url = "https://qyapi.weixin.qq.com/cgi-bin/card/batchget?access_token={$a_token}";
            return $this->common->httpRequest($url, $data, 'post');
        }
        
        /**
        * 修改卡券库存
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E5%8D%A1%E5%88%B8
        * 
        * @param mixed $card_id
        * @param mixed $nums
        * @param mixed $a_token
        */
        public function modifyStockCark($card_id, $nums = 0, $a_token = '') {
            if (empty($card_id)) return false;
            if ($nums == 0) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/card/modifystock?access_token={$a_token}";
            $data = array('card_id' => $card_id);
            if ($nums > 0) {
                $data['increase_stock_value'] = $nums;
            }else{
                $data['reduce_stock_value'] = -1*$nums;
            }
            return $this->common->httpRequest($url, $data, 'post');
        }
        
        /**
        * 删除卡券
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E5%8D%A1%E5%88%B8
        * 
        * @param mixed $card_id
        * @param AccessToken $a_token
        */
        public function deleteCard($card_id, $a_token = '') {
            if (empty($card_id)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/card/delete?access_token={$a_token}";
            $data = array('card_id' => $card_id);
            return $this->common->httpRequest($url, $data, 'post');
        }
        
        /**
        * 发送卡券消息
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E6%8A%95%E6%94%BE%E5%8D%A1%E5%88%B8
        * 
        * @param mixed $ary
        * @param AccessToken $a_token
        */
        public function sendCard($ary = array(), $a_token = '') {
            if (empty($ary)) return false;
            if (!isset($ary['msgtype']) || ($ary['msgtype'] != 'card') || !isset($ary['agentid']) || !isset($ary['card_id'])) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={$a_token}";
            return $this->common->httpRequest($url, $ary, 'post');
        }
        
        /**
        * 获取卡券图文消息内容
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E6%8A%95%E6%94%BE%E5%8D%A1%E5%88%B8
        * 
        * @param mixed $agentid
        * @param mixed $card_id
        * @param mixed $a_token
        */
        public function getCardHtml($agentid, $card_id, $a_token = '') {
            if (empty($agentid) || empty($card_id)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $url = "https://qyapi.weixin.qq.com/cgi-bin/card/mpnews/gethtml?access_token={$a_token}";
            $data = array('agentid' => $agentid, 'card_id' => $card_id);
            return $this->common->httpRequest($url, $data, 'post');
        }
        /**
        * 创建卡券二维码
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E6%8A%95%E6%94%BE%E5%8D%A1%E5%88%B8
        * 
        * @param mixed $ary
        * @param mixed $a_token
        */
        public function createCardQrcode($card_id, $expire = 1800, $openid = '', $unique = false, $outer_id = 0, $a_token = '') {
            if (empty($card_id)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $data = array('action_name' => 'QR_CARD');
            if ($expire == -1) {
                
            }else{
                if ($expire<60) $expire = 60;
                if ($expire>1800) $expire = 1800;
                $data['expire_seconds'] = $expire;
            }
            $card = array('card_id' => $card_id);
            if (!empty($openid)) $card['openid'] = $openid;
            if ($unique) $card['is_unique_code'] = true;
            if ($outer_id) $card['outer_id '] = $outer_id;
            $data['action_info']['card'] = $card;
            
            $url = "https://qyapi.weixin.qq.com/cgi-bin/card/qrcode/create?access_token={$a_token}";
            return $this->common->httpRequest($url, $data, 'post');
        }
        
        /**
        * 查询code
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E6%A0%B8%E9%94%80%E5%8D%A1%E5%88%B8
        * 
        * @param mixed $code
        * @param mixed $a_token
        */
        public function getCodeCardInfo($code, $a_token = '') {
            if (empty($code)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $data = array('code' => $code);
            $url = "https://qyapi.weixin.qq.com/cgi-bin/card/code/get?access_token={$a_token}";
            return $this->common->httpRequest($url, $data, 'post');
        }
        
        /**
        * 核销code
        * @see http://qydev.weixin.qq.com/wiki/index.php?title=%E6%A0%B8%E9%94%80%E5%8D%A1%E5%88%B8
        * 
        * @param mixed $code
        * @param AccessToken $a_token
        */
        public function consumeCodeCard($code, $a_token = '') {
            if (empty($code)) return false;
            if (empty($a_token)) $a_token = $this->common->read_access_token();
            $data = array('code' => $code);
            $url = "https://qyapi.weixin.qq.com/cgi-bin/card/code/consume?access_token={$a_token}";
            return $this->common->httpRequest($url, $data, 'post');
        }
    }
}